'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async fieldcreate() {
    const { ctx } = this;
    const { fieldAdress, suitable, introduce, fieldType, uid, fieldname, pictureurl } = ctx.request.body;
    if (!(fieldAdress && suitable && introduce && fieldType && uid && fieldname && pictureurl)) {
      return ctx.body = { success: false, msg: '请传入必要参数' };
    }
    try {
      const res = await ctx.model.Field.create({
        fieldAdress,
        suitable,
        introduce,
        fieldType,
        fieldname,
        uid,
        fieldTime: Date.now(),
        browse: 0,
      });
      const newurl = pictureurl.split(',');
      for (const item of newurl) {
        await ctx.model.Picture.create({
          fieldid: res.fieldid,
          photourl: item,
        });
      }
      ctx.body = { success: true, msg: '发布成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '发布失败' };
    }
  }

  async fieldupdate() {
    const { ctx } = this;
    const { fieldAdress, suitable, introduce, fieldType, fieldid, fieldname, browse } = ctx.request.body;
    if (!fieldid) {
      return ctx.body = { success: false, msg: '请传入需要修改的id' };
    }
    const update = {};
    if (fieldAdress) {
      update.fieldAdress = fieldAdress;
    }
    if (browse) {
      update.browse = browse;
    }
    if (suitable) {
      update.suitable = suitable;
    }
    if (introduce) {
      update.introduce = introduce;
    }
    if (fieldType) {
      update.fieldType = fieldType;
    }
    if (fieldname) {
      update.fieldname = fieldname;
    }
    try {
      await ctx.model.Field.update(
        update,
        {
          where: {
            fieldid,
          },
        }
      );
      ctx.body = { success: true, msg: '修改成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '修改失败' };
    }
  }

  async fieldfind() {
    const { ctx } = this;
    const { fieldAdress, suitable, fieldType, fieldid, page, uid } = ctx.request.body;
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    const where = {};
    if (fieldAdress) {
      where.fieldAdress = fieldAdress;
    }
    if (suitable) {
      where.suitable = suitable;
    }
    if (fieldType) {
      where.fieldType = fieldType;
    }
    if (fieldid) {
      where.fieldid = fieldid;
    }
    if (uid) {
      where.uid = uid;
    }
    try {
      await ctx.model.Field.hasMany(ctx.model.Picture, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasMany(ctx.model.Thumbs, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasMany(ctx.model.Comment, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasMany(ctx.model.Collection, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      const res = await ctx.model.Field.findAndCountAll({
        where,
        limit,
        offset,
        include: [{
          model: ctx.model.Picture,
        }, {
          model: ctx.model.Thumbs,
        }, {
          model: ctx.model.Collection,
        }, {
          model: ctx.model.Comment,
        }, {
          model: ctx.model.User,
          attributes: {
            exclude: [ 'pas' ],
          },
        }],
        order: [[ 'fieldid', 'DESC' ]],
      });
      const length = await ctx.model.Field.findAll({
        where,
      });
      ctx.body = { success: true, data: res, length: length.length, maxpage: Math.ceil(length.length / limit) };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败 ' };
    }
  }

  async fielddelete() {
    const { ctx } = this;
    const { fieldid } = ctx.request.body;
    if (!fieldid) {
      return ctx.body = { success: false, msg: '请传入地址id' };
    }
    try {
      await ctx.model.Field.destroy({
        where: {
          fieldid,
        },
      });
      ctx.body = { success: true, msg: '删除成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '删除失败' };
    }
  }

  async fieldlike() {
    const { ctx, app } = this;
    const { Op } = app.Sequelize;
    const { search, page } = ctx.request.body;
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    try {
      await ctx.model.Field.hasMany(ctx.model.Picture, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasMany(ctx.model.Thumbs, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasMany(ctx.model.Comment, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasMany(ctx.model.Collection, {
        foreignKey: 'fieldid',
        sourceKey: 'fieldid',
      });
      await ctx.model.Field.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      const res = await ctx.model.Field.findAndCountAll({
        where: {
          [Op.or]: [
            {
              fieldname: {
                [Op.like]: `%${search}%`,
              },
            },
            {
              fieldAdress: {
                [Op.like]: `%${search}%`,
              },
            },
          ],
        },
        limit,
        offset,
        include: [{
          model: ctx.model.Picture,
        }, {
          model: ctx.model.Thumbs,
        }, {
          model: ctx.model.Collection,
        }, {
          model: ctx.model.Comment,
        }, {
          model: ctx.model.User,
          attributes: {
            exclude: [ 'pas' ],
          },
        }],
        order: [[ 'fieldid', 'DESC' ]],
      });
      const length = await ctx.model.Field.findAll({
        where: {
          [Op.or]: [
            {
              fieldname: {
                [Op.like]: `%${search}%`,
              },
            },
            {
              fieldAdress: {
                [Op.like]: `%${search}%`,
              },
            },
          ],
        },
      });
      ctx.body = { success: true, data: res, length: length.length, maxpage: Math.ceil(length.length / limit) };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败 ' };
    }
  }
}

module.exports = HomeController;
